package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.Contact;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db = new DBHelp<Contact>();
	
	private class ContactRowMapper implements RowMapper<Contact> {

		public Contact mapperRow(ResultSet rs) throws SQLException {
			Contact c = new Contact();
			c.setId(rs.getString("id"));
			c.setName(rs.getString("name"));
			c.setCompanyname(rs.getString("companyname"));
			c.setMobile(rs.getString("mobile"));
			c.setTel(rs.getString("tel"));
			c.setEmail(rs.getString("email"));
			c.setAddress(rs.getString("address"));
			c.setWebsite(rs.getString("website"));
			c.setWeibo(rs.getString("weibo"));
			c.setContent(rs.getString("content"));
			c.setCreatetime(rs.getString("createtime"));
			c.setProjectId(rs.getString("projectId"));
			return c;
		}
	}
	/**
	 * 
	 * 通过项目id 查找联系人列表
	 * @param projectId 项目id
	 * @return 联系人列表
	 */
	public List<Contact> findContactsByProjectId(String projectId) {
		String sql = "select id,name,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid from t_contact where projectid = ?";
		return db.executeQueryForList(sql, new ContactRowMapper(), projectId);
	}
	/**
	 * 通过联系人id查找联系人
	 * @param contactId 联系人id
	 * @return 联系人
	 */
	public Contact findContactByContactId(String contactId) {
		String sql = "select id,name,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid from t_contact where id = ?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), contactId);
	}
	/**
	 * 保存联系人
	 * @param projectId 
	 * @param contact
	 */
	public void saveContact(Contact c, String projectId) {
		String sql = "insert into t_contact(id,name,companyname,mobile,tel,email,address,website,weibo,content,createtime,projectid) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getName(),c.getCompanyname(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getCreatetime(),projectId);
	}
	/**
	 * 通过联系人id和项目id删除联系人
	 * @param contactId
	 */
	public void delContact(String contactId) {
		String sql = "delete from t_contact where id = ?";
		db.executeSQL(sql, contactId);
	}
	/**
	 * 更新联系人
	 * @param contact
	 */
	public void updateContact(Contact c) {
		String sql = "update t_contact set name=?,companyname=?,mobile=?,tel=?,email=?,address=?,website=?,weibo=?,content=? where id=?";
		db.executeSQL(sql, c.getName(),c.getCompanyname(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getId());
	}
}
